package com.speaktoitcookbook.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;

import com.speaktoitcookbook.bean.Material;
import com.speaktoitcookbook.dao.IMaterialDao;
import com.speaktoitcookbook.dao.factory.DaoFactory;

@WebServlet("/CookMaterialService")
public class CookMaterialService extends HttpServlet{
	private static final long serialVersionUID = 1L;
	int cbID = 0;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
       doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		
		cbID = -1;
		try {
			cbID = Integer.parseInt(request.getParameter("cb"));
		} catch (Exception e) {
		}

		PrintWriter out = response.getWriter();
		
		IMaterialDao materialDao=DaoFactory.getMaterialDao();
		Material material=new Material();
		material.setID(cbID);
		List<Material> materiallist = null;

		try {
			materiallist = materialDao.getMaterial(cbID);
		} catch (SQLException e) {
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("materiallist:" + materiallist);
		JSONArray newsjson = new JSONArray(materiallist, false);
		out.print(newsjson.toString());
		out.flush();
		out.close();
	}
	
	

}